package com.xx.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xx.entity.Category;
import com.xx.entity.Dish;
import com.xx.service.IDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  菜品控制器
 * </p>
 *
 * @author myx
 * @since 2025-01-09
 */
@RestController
@CrossOrigin
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private IDishService dishService;

    /**
     * 获取所有菜品及其分类信息
     * @return 菜品和分类信息列表
     */
    @GetMapping("/with-category")
    public List<Dish> getAllDishesWithCategory() {
        return dishService.getAllDishesWithCategory();
    }

    /**
     * 分页获取菜品及其分类信息
     * @param pageNum 当前页码
     * @param pageSize 每页大小
     * @return 分页信息
     */
    @GetMapping("/page")
    public Page<Dish> getDishesWithCategoryPage(@RequestParam int pageNum,
                                                @RequestParam(defaultValue = "3") int pageSize,
                                                @RequestParam(required = false) String dishName) {
        return dishService.getDishesWithCategoryPage(pageNum, pageSize,dishName);
    }

    /**
     * 新增菜品
     * @param dish 菜品对象
     */
    @PostMapping
    public void addDish(@RequestBody Dish dish) {
        dishService.addDish(dish);
    }

    /**
     * 通过菜名模糊查询菜品
     * @param name 菜名
     * @return 菜品列表
     */
    @GetMapping("/search")
    public List<Dish> searchDishesByName(@RequestParam String name) {
        return dishService.searchDishesByName(name);
    }

    /**
     * 通过ID查询菜品信息
     * @param id 菜品ID
     * @return 菜品对象
     */
    @GetMapping("/{id}")
    public Dish getDish(@PathVariable Integer id) {
        return dishService.getDishById(id);
    }

    /**
     * 更新菜品信息
     * @param dish 菜品对象
     */
    @PutMapping
    public void updateCategory(@RequestBody Dish dish){
        dishService.updateDish(dish);
    }

    /**
     * 根据ID删除菜品
     * @param id 菜品ID
     */
    @DeleteMapping("/{id}")
    public void deleteStudent(@PathVariable Integer id) {
        dishService.delDishById(id);
    }
}
